{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Notebook for Ex-Post Analysis of polynomial Models "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "mk, 16.7.2021"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Packages and Options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "fn_coefs = \"../results/table_coefficients/df_coefs_lags12.csv\"\n",
    "fn_gen_agg = \"../data/generation_aggregated.csv\"\n",
    "fn_cost = \"../data/RES_Subsidies_DE_and_UK.xlsx\"\n",
    "fn_out = \"../results/results_poly_submission.xlsx\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir_covs = \"../results/cov_for_SE/\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Choose options**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Choose phi tilde\n",
    "phi_tilde = 0.74\n",
    "\n",
    "# Choose models\n",
    "Models = [\"poly_12\"]         \n",
    "\n",
    "# Dummy for linear specification\n",
    "L = \"no\" \n",
    "\n",
    "# Only use significant coefficients\n",
    "significance = \"yes\" \n",
    "\n",
    "# Start and end date\n",
    "tstart = \"1.1.2015\"\n",
    "tend = \"29.12.2016\"\n",
    "\n",
    "# Define regimes and technologies\n",
    "regimes = [\"low\", \"high\"]\n",
    "res = [\"wind\", \"solar\"]\n",
    "tech = [\"coal\",\"ccgt\"]\n",
    "\n",
    "#  Estimates for Social Cost of Carbon and Levelized Cost of Electricity\n",
    "scc = 50 # €/t\n",
    "lcoe = {'RE': [\"wind\", \"solar\"], 'lcoe': [100,100]} # €/MWh\n",
    "df_lcoe = pd.DataFrame(data=lcoe)\n",
    "\n",
    "# Carbon coefficient per MWh electricity generated\n",
    "d = {'tech': [\"coal\",\"ccgt\"], 'e': [0.89,0.38]} # t CO2 per MWh_el\n",
    "df_e = pd.DataFrame(data=d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load and prepare data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get estimated coefficients as well as input data used for estimations.\n",
    "df_coefs_in = pd.read_csv(fn_coefs)\n",
    "df_coefs_in = df_coefs_in[df_coefs_in.Model.isin(Models)]\n",
    "df_gen_agg = pd.read_csv(fn_gen_agg, parse_dates=True, index_col=\"date\")[tstart:tend].copy()\n",
    "df_cost = pd.read_excel(fn_cost, sheet_name=\"support\")\n",
    "\n",
    "# Covariance Matrix\n",
    "covs = {}\n",
    "for Model in Models:\n",
    "    for t in tech:\n",
    "        df_cov = pd.read_csv(dir_covs + \"%s_fpn_%s_cov.csv\" % (Model, t))\n",
    "        df_cov = df_cov.set_index(\"index\")\n",
    "        df_cov.index.names = [None] \n",
    "        covs.update( {\"%s_fpn_%s\" % (Model, t) : df_cov} )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare data frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from get_results_ import get_hourly_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>demand</th>\n",
       "      <th>wind</th>\n",
       "      <th>solar</th>\n",
       "      <th>w_wind</th>\n",
       "      <th>w_solar</th>\n",
       "      <th>w_demand</th>\n",
       "      <th>pele_uk</th>\n",
       "      <th>pcarb</th>\n",
       "      <th>subsidy_wind_paid</th>\n",
       "      <th>subsidy_solar_paid</th>\n",
       "      <th>subsidy_wind_mwh</th>\n",
       "      <th>subsidy_solar_mwh</th>\n",
       "      <th>regime</th>\n",
       "      <th>wsubsidy_wind_mwh</th>\n",
       "      <th>wsubsidy_solar_mwh</th>\n",
       "      <th>wpele_wind_mwh</th>\n",
       "      <th>wpele_solar_mwh</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-01 00:00:00</th>\n",
       "      <td>17791.516667</td>\n",
       "      <td>6989.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000111</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000046</td>\n",
       "      <td>39.61</td>\n",
       "      <td>19.150881</td>\n",
       "      <td>472192.394807</td>\n",
       "      <td>0.0</td>\n",
       "      <td>67.557392</td>\n",
       "      <td>155.19</td>\n",
       "      <td>low</td>\n",
       "      <td>0.020344</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.011928</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-01 01:00:00</th>\n",
       "      <td>17489.516667</td>\n",
       "      <td>6971.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000111</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000045</td>\n",
       "      <td>37.18</td>\n",
       "      <td>19.150881</td>\n",
       "      <td>470942.583046</td>\n",
       "      <td>0.0</td>\n",
       "      <td>67.557392</td>\n",
       "      <td>155.19</td>\n",
       "      <td>low</td>\n",
       "      <td>0.020291</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.011167</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           demand    wind  solar    w_wind  w_solar  w_demand  \\\n",
       "date                                                                            \n",
       "2015-01-01 00:00:00  17791.516667  6989.5    0.0  0.000111      0.0  0.000046   \n",
       "2015-01-01 01:00:00  17489.516667  6971.0    0.0  0.000111      0.0  0.000045   \n",
       "\n",
       "                     pele_uk      pcarb  subsidy_wind_paid  \\\n",
       "date                                                         \n",
       "2015-01-01 00:00:00    39.61  19.150881      472192.394807   \n",
       "2015-01-01 01:00:00    37.18  19.150881      470942.583046   \n",
       "\n",
       "                     subsidy_solar_paid  subsidy_wind_mwh  subsidy_solar_mwh  \\\n",
       "date                                                                           \n",
       "2015-01-01 00:00:00                 0.0         67.557392             155.19   \n",
       "2015-01-01 01:00:00                 0.0         67.557392             155.19   \n",
       "\n",
       "                    regime  wsubsidy_wind_mwh  wsubsidy_solar_mwh  \\\n",
       "date                                                                \n",
       "2015-01-01 00:00:00    low           0.020344                 0.0   \n",
       "2015-01-01 01:00:00    low           0.020291                 0.0   \n",
       "\n",
       "                     wpele_wind_mwh  wpele_solar_mwh  \n",
       "date                                                  \n",
       "2015-01-01 00:00:00        0.011928              0.0  \n",
       "2015-01-01 01:00:00        0.011167              0.0  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#phi_tilde = 0.74\n",
    "df_dem = get_hourly_df(df_gen_agg, phi_tilde, df_cost, regimes, res)\n",
    "df_dem.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Extract estimation results and calculate generation offset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare dataframe to calculate replacement effects"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Extract and rename RE-coefficients to calcuate average marginal replacement effect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "map_coefs = {\"low_wind\": \"beta1\", \"low_solar\": \"beta1\",\n",
    "             \"high_wind\": \"beta1\", \"high_solar\": \"beta1\",\n",
    "             \"low_wind_wind\": \"beta2\", \"low_solar_solar\": \"beta2\",\n",
    "             \"high_wind_wind\": \"beta2\", \"high_solar_solar\": \"beta2\",\n",
    "             \"low_wind_wind_wind\": \"beta3\", \"low_solar_solar_solar\": \"beta3\",\n",
    "             \"high_wind_wind_wind\": \"beta3\", \"high_solar_solar_solar\": \"beta3\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>tech</th>\n",
       "      <th>RE</th>\n",
       "      <th>regime</th>\n",
       "      <th>Model</th>\n",
       "      <th>beta1</th>\n",
       "      <th>beta2</th>\n",
       "      <th>beta3</th>\n",
       "      <th>demand</th>\n",
       "      <th>solar</th>\n",
       "      <th>wind</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>ccgt</td>\n",
       "      <td>solar</td>\n",
       "      <td>high</td>\n",
       "      <td>poly_12</td>\n",
       "      <td>-0.575778</td>\n",
       "      <td>-0.000018</td>\n",
       "      <td>0.0</td>\n",
       "      <td>21814.936489</td>\n",
       "      <td>973.502201</td>\n",
       "      <td>3699.261354</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ccgt</td>\n",
       "      <td>wind</td>\n",
       "      <td>high</td>\n",
       "      <td>poly_12</td>\n",
       "      <td>-0.746952</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>21814.936489</td>\n",
       "      <td>973.502201</td>\n",
       "      <td>3699.261354</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   tech     RE regime    Model     beta1     beta2  beta3        demand  \\\n",
       "0  ccgt  solar   high  poly_12 -0.575778 -0.000018    0.0  21814.936489   \n",
       "1  ccgt   wind   high  poly_12 -0.746952  0.000000    0.0  21814.936489   \n",
       "\n",
       "        solar         wind  \n",
       "0  973.502201  3699.261354  \n",
       "1  973.502201  3699.261354  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# We extract coefficients and standard errors.\n",
    "df_coefs = df_coefs_in.copy()\n",
    "df_coefs.tech = df_coefs.tech.map(lambda x: x.split(\"_\")[-1])\n",
    "df_coefs[\"regime\"] = df_coefs.variable.map(lambda x: x.split(\"_\")[0])\n",
    "df_coefs[\"RE\"] = df_coefs.variable.map(lambda x: x.split(\"_\")[1])\n",
    "\n",
    "# Rename coefficients\n",
    "df_ = df_coefs.copy()\n",
    "df_ = df_[df_.variable.isin(map_coefs.keys())].copy()\n",
    "df_[\"beta\"] = df_.variable.map(map_coefs)\n",
    "df_ = df_.drop(\"variable\", axis=1)\n",
    "cols = [\"beta\", \"tech\", \"RE\", \"regime\", \"Model\"]\n",
    "value_vars = [\"coef\", \"std\", \"pvalue\"]\n",
    "\n",
    "if significance == \"yes\":\n",
    "    df_.loc[df_.pvalue>0.1, \"coef\"] = 0\n",
    "\n",
    "df_ = pd.melt(df_, id_vars=cols, value_vars=value_vars)\n",
    "df_ = df_[df_.variable == \"coef\"\n",
    "                ].drop(\"variable\", axis=1\n",
    "                      ).pivot_table(\"value\", [\"tech\", \"RE\", \"regime\", \"Model\"], \"beta\").reset_index()\n",
    "\n",
    "# Add mean values of wind, solar and demand per regime to calculate replacement effects\n",
    "df_2 = df_.copy()\n",
    "df_2 = df_2.merge(df_dem.groupby(\"regime\", as_index=False).demand.mean(), on=\"regime\")\n",
    "df_2 = df_2.merge(df_dem.groupby(\"regime\", as_index=False).solar.mean(), on=\"regime\")\n",
    "df_2 = df_2.merge(df_dem.groupby(\"regime\", as_index=False).wind.mean(), on=\"regime\")\n",
    "df_2.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This function needs to be adapted for other specifications!\n",
    "def get_standard_error(Model, tech, demand, regime, RE, gen_RE):\n",
    "    \"\"\"function to calculate standard errors of marginal effects evaluated at mean demand (\"Delta_q\")\n",
    "    :param Model: <string> with Model name\n",
    "    :param tech: <string> with fossil technology\n",
    "    :param demand_mean: mean demand\n",
    "    :param regime: <string> with regime\n",
    "    :param RE: <string> with renewable energy technology\n",
    "    :return se: <float> with regression results\"\"\"\n",
    "    \n",
    "    df_cov = covs[\"%s_fpn_%s\" % (Model, tech)]\n",
    "    coefs = [\"%s_%s\" % (regime, RE), \"%s_%s_%s\" % (regime, RE, RE), \"%s_%s_%s_%s\" % (regime, RE, RE, RE)]\n",
    "#    coefs = [\"%s_%s\" % (regime, RE), \"%s_%s_dem\" % (regime, RE)]\n",
    "    \n",
    "    gen_RE2 = gen_RE*gen_RE\n",
    "    weights = pd.Series(index=coefs, data = [1, gen_RE, gen_RE2])\n",
    "#    weights = pd.Series(index=coefs, data = [1, demand])\n",
    "\n",
    "    weights_m = weights.values\n",
    "    cov = df_cov.loc[coefs, coefs].values\n",
    "    se = np.sqrt(weights_m.dot(cov).dot(weights_m))\n",
    "    \n",
    "    return se"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This function needs to be adapted for other specifications!\n",
    "def get_df_Q(df, RE):\n",
    "    df_ = df.copy()\n",
    "\n",
    "    df_RE = df_.loc[df_.RE==RE].copy()\n",
    "#    df_RE[\"deltaQ\"] = df_RE.beta1 + df_RE.beta4*df_RE.demand\n",
    "    df_RE[\"deltaQ\"] = df_RE.beta1 + 2*df_RE.beta2*df_RE[RE]+ 3*df_RE.beta3*df_RE[RE]*df_RE[RE]\n",
    "    df_RE[\"deltaQ_se\"] = df_RE.apply(lambda row: get_standard_error(\n",
    "        row[\"Model\"], row[\"tech\"], row[\"demand\"], row[\"regime\"], row[\"RE\"], row[RE]), axis=1)\n",
    "\n",
    "    return df_RE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculate generation results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "df_RE[\"deltaQ\"] = df_RE.beta1 + 2*df_RE.beta2*df_RE[RE] + df_RE.beta3*df_RE.demand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>tech</th>\n",
       "      <th>RE</th>\n",
       "      <th>Model</th>\n",
       "      <th>regime</th>\n",
       "      <th>deltaQ</th>\n",
       "      <th>deltaQ_se</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ccgt</td>\n",
       "      <td>wind</td>\n",
       "      <td>poly_12</td>\n",
       "      <td>high</td>\n",
       "      <td>-0.746952</td>\n",
       "      <td>0.030629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>coal</td>\n",
       "      <td>wind</td>\n",
       "      <td>poly_12</td>\n",
       "      <td>high</td>\n",
       "      <td>-0.253043</td>\n",
       "      <td>0.030629</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   tech    RE    Model regime    deltaQ  deltaQ_se\n",
       "1  ccgt  wind  poly_12   high -0.746952   0.030629\n",
       "3  coal  wind  poly_12   high -0.253043   0.030629"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if L == \"yes\":\n",
    "    RE = \"wind\"\n",
    "    df_Q_wind = get_df_Q_linear(df_2, RE)\n",
    "    RE = \"solar\"\n",
    "    df_Q_solar = get_df_Q_linear(df_2, RE)\n",
    "    df_Q = pd.concat([df_Q_wind,df_Q_solar])\n",
    "    \n",
    "else: \n",
    "    RE = \"wind\"\n",
    "    df_Q_wind = get_df_Q(df_2, RE)\n",
    "    RE = \"solar\"\n",
    "    df_Q_solar = get_df_Q(df_2, RE)\n",
    "    df_Q = pd.concat([df_Q_wind,df_Q_solar])\n",
    "\n",
    "df_Q = df_Q[[\"tech\", \"RE\", \"Model\", \"regime\", \"deltaQ\", \"deltaQ_se\"]]\n",
    "df_Q.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculate emission offset and cost data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from get_results_ import get_final_results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RE</th>\n",
       "      <th>regime</th>\n",
       "      <th>Model</th>\n",
       "      <th>deltaE</th>\n",
       "      <th>deltaE_se</th>\n",
       "      <th>pcarb</th>\n",
       "      <th>market_carbon_rent</th>\n",
       "      <th>market_carbon_rent_se</th>\n",
       "      <th>meb</th>\n",
       "      <th>meb_se</th>\n",
       "      <th>premium</th>\n",
       "      <th>premium_se</th>\n",
       "      <th>market_income</th>\n",
       "      <th>operating_cost</th>\n",
       "      <th>feed_in</th>\n",
       "      <th>premium_paid</th>\n",
       "      <th>premium_paid_t</th>\n",
       "      <th>lcoe</th>\n",
       "      <th>wpele</th>\n",
       "      <th>MAC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>solar</td>\n",
       "      <td>high</td>\n",
       "      <td>poly_12</td>\n",
       "      <td>-0.578481</td>\n",
       "      <td>0.026854</td>\n",
       "      <td>28.534708</td>\n",
       "      <td>16.506781</td>\n",
       "      <td>0.766270</td>\n",
       "      <td>28.924041</td>\n",
       "      <td>1.342698</td>\n",
       "      <td>12.417260</td>\n",
       "      <td>1.102573</td>\n",
       "      <td>38.349266</td>\n",
       "      <td>21.842485</td>\n",
       "      <td>50.766526</td>\n",
       "      <td>62.577734</td>\n",
       "      <td>108.175987</td>\n",
       "      <td>100</td>\n",
       "      <td>38.349266</td>\n",
       "      <td>106.573513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>solar</td>\n",
       "      <td>low</td>\n",
       "      <td>poly_12</td>\n",
       "      <td>-0.554200</td>\n",
       "      <td>0.033314</td>\n",
       "      <td>28.713926</td>\n",
       "      <td>15.913262</td>\n",
       "      <td>0.956581</td>\n",
       "      <td>27.710007</td>\n",
       "      <td>1.665710</td>\n",
       "      <td>11.796745</td>\n",
       "      <td>1.363650</td>\n",
       "      <td>43.286753</td>\n",
       "      <td>27.373491</td>\n",
       "      <td>55.083498</td>\n",
       "      <td>155.190000</td>\n",
       "      <td>280.025187</td>\n",
       "      <td>100</td>\n",
       "      <td>43.286753</td>\n",
       "      <td>102.333512</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      RE regime    Model    deltaE  deltaE_se      pcarb  market_carbon_rent  \\\n",
       "0  solar   high  poly_12 -0.578481   0.026854  28.534708           16.506781   \n",
       "1  solar    low  poly_12 -0.554200   0.033314  28.713926           15.913262   \n",
       "\n",
       "   market_carbon_rent_se        meb    meb_se    premium  premium_se  \\\n",
       "0               0.766270  28.924041  1.342698  12.417260    1.102573   \n",
       "1               0.956581  27.710007  1.665710  11.796745    1.363650   \n",
       "\n",
       "   market_income  operating_cost    feed_in  premium_paid  premium_paid_t  \\\n",
       "0      38.349266       21.842485  50.766526     62.577734      108.175987   \n",
       "1      43.286753       27.373491  55.083498    155.190000      280.025187   \n",
       "\n",
       "   lcoe      wpele         MAC  \n",
       "0   100  38.349266  106.573513  \n",
       "1   100  43.286753  102.333512  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_deltaE = get_final_results(df_Q, df_dem, df_e, regimes, res, scc, df_lcoe)\n",
    "df_deltaE.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Export"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "wxls = pd.ExcelWriter(fn_out)\n",
    "df_deltaE.melt(id_vars=[\"RE\", \"regime\", \"Model\"]).to_excel(wxls, index=False, sheet_name=\"coefs_Delta_E\")\n",
    "df_Q.melt(id_vars=[\"RE\", \"regime\", \"Model\", \"tech\"]).to_excel(wxls, index=False, sheet_name=\"coefs_Delta_Q\")\n",
    "wxls.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
